package com.atguigu.yygh.cos.service.impl;

import com.atguigu.yygh.cos.service.FileService;
import com.atguigu.yygh.cos.util.CosServiceUtil;
import com.atguigu.yygh.cos.util.MultipartFileToFile;
import com.qcloud.cos.COSClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private CosServiceUtil cosServiceUtil;


    @Override
    public String upload(MultipartFile file) {
        String url = null ;
        // 1、创建一个 Cos 客户端
        COSClient cosClient = cosServiceUtil.getCosClient();
        // 2、上传文件对象并关闭客户端，得到文件路径URL
        /* MultipartFile -->> File */
        try {
            File toFile = MultipartFileToFile.multipartFileToFile(file);
            url = cosServiceUtil.uploadFile(cosClient, toFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;

    }
}
